/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.classdiagram.parsers.texteditor.parameterlabel;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.graphics.Color;

import com.cea.papyrus.classdiagram.parsers.texteditor.ILabelColorConstants;
import com.cea.papyrus.classdiagram.parsers.texteditor.LabelColorProvider;
import com.cea.papyrus.umlutils.Parameter;

/**
 * This class bundles the configuration space of the parameter label editor
 * Instances of this class are passed to the configure method of
 * <code>SourceViewer</code>.
 * 
 * @author Remi SCHNEKENBURGER
 * @see org.eclipse.jface.text.source.SourceViewerConfiguration
 */
public class ParameterLabelSourceViewerConfiguration extends SourceViewerConfiguration {
	
	/** edited parameter */
	private Parameter parameter;
	
	/**
	 * Creates a new ParameterLabelSourceViewerConfiguration.
	 * @param parameter the parameter to edit
	 */
	public ParameterLabelSourceViewerConfiguration(org.eclipse.uml2.uml.Parameter parameter) {
		this.parameter = new Parameter(parameter);
	}
	
	/**
	 * Creates a new ParameterLabelSourceViewerConfiguration.
	 */
	public ParameterLabelSourceViewerConfiguration() {
		this(null);
	}
	
	public void setParameter(org.eclipse.uml2.uml.Parameter parameter) {
		this.parameter = new Parameter(parameter);
	}
	
	public org.eclipse.uml2.uml.Parameter getParameter() {
		return parameter.getUml2Parameter();
	}
    /**
     * 
     */
    static class SingleTokenScanner extends BufferedRuleBasedScanner {
		
		/**
		 * 
		 * 
		 * @param attribute 
		 */
		public SingleTokenScanner(TextAttribute attribute) {
			setDefaultReturnToken(new Token(attribute));
		}
	};

	/* (non-Javadoc)
	 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getPresentationReconciler(org.eclipse.jface.text.source.ISourceViewer)
	 */
	/**
	 * 
	 * 
	 * @param sourceViewer 
	 * 
	 * @return 
	 */
	public IPresentationReconciler getPresentationReconciler(
			ISourceViewer sourceViewer) {
		PresentationReconciler reconciler = new PresentationReconciler();

		// creating damager/repairer for single line comments
		Color colorString = LabelColorProvider.getDefault().getColor(ILabelColorConstants.STRING);
		DefaultDamagerRepairer dr = new DefaultDamagerRepairer(
				new SingleTokenScanner(
						new TextAttribute(colorString)));

		reconciler.setDamager(dr, ParameterLabelPartitionScanner.PARAMETER_MODIFIERS_STRING);
		reconciler.setRepairer(dr, ParameterLabelPartitionScanner.PARAMETER_MODIFIERS_STRING);

		// creating damager/repairer for other zones (action language zone)
		dr = new DefaultDamagerRepairer(new ParameterLabelScanner());
		reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
		reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);

		return reconciler;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getContentAssistant(org.eclipse.jface.text.source.ISourceViewer)
	 */
	/**
	 * 
	 * 
	 * @param sourceViewer 
	 * 
	 * @return 
	 */
	public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
		ContentAssistant assistant = new ContentAssistant();

		IContentAssistProcessor processor = new ParameterLabelCompletionProcessor(parameter);
		assistant.setContentAssistProcessor(processor,IDocument.DEFAULT_CONTENT_TYPE);

		// IInformationControlCreator
		assistant.setInformationControlCreator(
		        getInformationControlCreator(sourceViewer));
		
		assistant.enableAutoActivation(true);
		assistant.setAutoActivationDelay(500);

		return assistant;
	}
}